/***********************************************************************
 *
 * MODULE NAME:    avdtp.h
 * DESCRIPTION:    a2dp profile platform interface
 * MAINTAINER:      Zhu Jianguo
 *
 * 
 * LICENSE:
 *     This source code is copyright (c) 2011 RDA Ltd.
 *     All rights reserved.
 *
 * REVISION HISTORY:
 * 
 *
 * ISSUES:
 * 
 ***********************************************************************/
#ifndef AVDTP_H_
#define AVDTP_H_

#ifdef __cplusplus
extern "C" {
#endif


/*!
	@brief Stream End Point role. 
*/
typedef enum
{
	SEP_SOURCE,				/*!< Stream Endpoint is the Source.*/
	SEP_SINK				/*!< Stream Endpoint is the Sink.*/
} t_SEP_ROLE; 



/*! 
	@brief The type of the SEP. This identifies the codec type being registered
	or used.
*/
typedef enum
{
	/*! Invalid SEP value. */
	A2DP_SEP_INVALID = 0,	
	/*! The SEP supports the SBC codec (support for this codec is
	  mandatory). */
	A2DP_SBC,				
	/*! The SEP supports the MPEG-1,2 AUdio codec. */
	A2DP_MPEG_AUDIO,		
	/*! The SEP supports the MPEG-2,4 AAC codec. */
	A2DP_MPEG_AAC,			
	/*! The SEP supports a codec from the ATRAC family. */
	A2DP_ATRAC,				
	/*! The SEP supports a vendor defined codec. */
	A2DP_VENDOR				
} t_A2DP_SEP_TYPE;

#define MAX_NUM_REMOTE_SEIDS 4
#define MAX_NUM_REMOTE_CAPABILITIES 4

#define MAX_NUM_LOCAL_SEIDS 1
#define MAX_NUM_LOCAL_CAPABILITIES 2

/* media_type */
#define BT_A2DP_MEDIA_AUDIO 		0
#define BT_A2DP_MEDIA_VIDEO 		1
#define BT_A2DP_MEDIA_MULTIMEDIA 	2
#define BT_A2DP_MEDIA_UNKNOWN 	0xff

/* sep_type */
#define BT_A2DP_SOURCE 	0
#define BT_A2DP_SINK 		1

/* codec_type */
#define BT_A2DP_SBC 		1
#define BT_A2DP_MP3 		2
#define BT_A2DP_AAC 		3
#define BT_A2DP_ATRAC 		4
#define BT_A2DP_NON_A2DP        5


/*!
	@brief The capability to stream media. This is manditory for advance audio
	distribution profile.
*/
#define AVDTP_SERVICE_MEDIA_TRANSPORT		(1)		
/*!
	@brief The reporting capability. This is not currently supported.
*/
#define AVDTP_SERVICE_REPORTING				(2)		
/*!
	@brief The recovery capability. This is not currently supported.
*/
#define AVDTP_SERVICE_RECOVERY				(3)		
/*!
	@brief The content protection capability. This is not currently supported.
*/
#define AVDTP_SERVICE_CONTENT_PROTECTION	(4)		
/*!
	@brief The header compression capability. This is not currently supported.
*/
#define AVDTP_SERVICE_HEADER_COMPRESSION	(5)		
/*!
	@brief The multiplexing capability. This is not currently supported.
*/
#define AVDTP_SERVICE_MULTIPLEXING			(6)		
/*!
	@brief The codec capability for the Stream End Point.
*/
#define AVDTP_SERVICE_MEDIA_CODEC			(7)		

typedef enum
{
    BT_A2DP_MESSAGE_TYPE_COMMAND = (0x0),
    BT_A2DP_MESSAGE_TYPE_ACCEPT = (0x02),
    BT_A2DP_MESSAGE_TYPE_REJECT = (0x03)
}enum_a2dp_signal_cmd_status;



#ifndef __BT_A2DP_CODEC_TYPES__
#define __BT_A2DP_CODEC_TYPES__
typedef struct
{
	u_int8 in_use;
	u_int8 seid;
	u_int8 media_type;
	u_int8 sep_type;
    u_int8 get_cap;
    u_int8 have_this_sep;
} bt_sep_info_struct;

typedef struct
{
	u_int8 min_bit_pool;
	u_int8 max_bit_pool;
	u_int8 block_len; // b0: 16, b1: 12, b2: 8, b3: 4
	u_int8 subband_num; // b0: 8, b1: 4
	u_int8 alloc_method; // b0: loudness, b1: SNR
	u_int8 sample_rate; // b0: 48000, b1: 44100, b2: 32000, b3: 16000
	u_int8 channel_mode; // b0: joint stereo, b1: stereo, b2: dual channel, b3: mono
} bt_a2dp_sbc_codec_cap_struct;

typedef struct
{
	u_int8 layer; // b0: layerIII, b1: layerII, b0: layerI
	u_int8 CRC;
	u_int8 channel_mode; // b0: joint stereo, b1: stereo, b2: dual channel, b3: mono
	u_int8 MPF; // is support MPF-2
	u_int8 sample_rate; // b0: 48000, b1: 44100, b2: 32000, b3: 24000, b4: 22050, b5: 16000
	u_int8 VBR; // is support VBR
	u_int16 bit_rate; // bit-rate index in ISO 11172-3 , b0:0000 ~ b14: 1110
} bt_a2dp_mp3_codec_cap_struct; /* all MPEG-1,2 Audio */

typedef struct
{
	u_int8 object_type; // b4: M4-scalable, b5: M4-LTP, b6: M4-LC, b7: M2-LC
	u_int16 sample_rate; // b0~b11: 96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000
	u_int8 channels; // b0: 2, b1: 1
	u_int8 VBR; // is supported VBR
	u_int32 bit_rate; // constant/peak bits per second in 23 bit UiMsbf, 0:unknown
} bt_a2dp_aac_codec_cap_struct; /* all MPEG-2,4 AAC */

typedef struct
{
	u_int8 version; // 1:ATRAC, 2:ATRAC2, 3:ATRAC3
	u_int8 channel_mode; // b0: joint stereo, b1: dual, b2: single
	u_int8 sample_rate; // b0: 48000, b1: 44100
	u_int8 VBR; // is supported VBR
	u_int32 bit_rate; // bit-rate index in ATRAC, b0: 0x0012 ~ b18: 0x0000
	u_int16 max_sul; // sound unit length in 16 bits UiMsbf
} bt_a2dp_atrac_codec_cap_struct; /* all ATRAC family */

typedef union
{
	bt_a2dp_sbc_codec_cap_struct sbc;
	bt_a2dp_mp3_codec_cap_struct mp3;
	bt_a2dp_aac_codec_cap_struct aac;
	bt_a2dp_atrac_codec_cap_struct atrac;
} bt_a2dp_audio_codec_cap_struct;

typedef struct
{
	u_int8 codec_type; // SBC, MP3
	bt_a2dp_audio_codec_cap_struct codec_cap;
} bt_a2dp_audio_cap_struct;
#endif

typedef struct
{
    u_int16 connect_id;
    t_bdaddr device_addr;
} t_a2dp_signal_connect_ind;

typedef struct
{
    u_int16 connect_id;
    u_int8 acp_seid;
} t_a2dp_capabilities_get_ind;

typedef struct
{
	u_int16 connect_id;
	u_int8 acp_seid;
	u_int8 int_seid;
	u_int8 stream_handle;
	bt_a2dp_audio_cap_struct audio_cap;
} t_a2dp_stream_config_ind;

typedef struct
{
	u_int16 connect_id;
	u_int16 result;
	u_int16 sep_num;
	bt_sep_info_struct sep_list[MAX_NUM_REMOTE_SEIDS];
} t_a2dp_sep_discover_cnf;

typedef struct
{
    u_int8   a2dp_supported;
    u_int16 supported_feature;
} t_a2dp_Supported_feature;

typedef struct 
{
	u_int16 cid;
	u_int16 result;
	t_bdaddr bdaddr;
}a2dp_connect_ind_msg_t;

typedef struct
{
    u_int16 connect_id;
    u_int8 acp_seid;
} t_a2dp_capabilities_get;

typedef struct
{
	u_int16 connect_id;
	u_int8 acp_seid;
	u_int8 int_seid;
	u_int8 stream_handle;
	bt_a2dp_audio_cap_struct audio_cap;
} t_a2dp_stream_config;

typedef struct
{
	u_int16 connect_id;
	u_int16 result;
	u_int16 sep_num;
	bt_sep_info_struct sep_list[MAX_NUM_REMOTE_SEIDS];
} t_a2dp_sep_discover;



void Avdtp_Init(u_int8 accept_avdtp);
void Avdtp_Register_Sep(t_A2DP_SEP_TYPE sep_type, u_int8 rate, u_int8 quality);
int AVDTP_Setup_Connect(t_bdaddr address);
u_int8 AVDTP_Start_Media_Channel_Connection(void);
int AVDTP_Disconnect(void);
void Avdtp_Disconnect_Media_Channel(void);

u_int8 Avdtp_Get_Codec_Type(void);

u_int8 Avdtp_Send_Reconfigure(u_int16 connect_id, u_int8 acp_seid, u_int8 int_seid, u_int8 codec_type,bt_a2dp_audio_codec_cap_struct* audio_cap);
u_int8 Avdtp_Send_SetConfiguration(u_int16 connect_id, u_int8 acp_seid, u_int8 int_seid, u_int8 codec_type, bt_a2dp_audio_codec_cap_struct* audio_cap);
u_int8 Avdtp_Send_Open(u_int8 acp_seid);

u_int8 Avdtp_Send_Discover(u_int16 cid);
u_int8 Aavdp_Send_GetCapabilities(u_int8 seid, u_int16 cid);

u_int8 Avdtp_Send_Start(void);
u_int8 Avdtp_Send_Suspend(void);
u_int8 Avdtp_Send_Close(void);
u_int8 Avdtp_Send_Abort(void);

int Avdtp_Send_Data(u_int32 length, u_int8* p_data, u_int32 timestamp,u_int16 seq_num, u_int8 payload_type, u_int8 marker , u_int8 frames);


void Avdtp_Send_Sep_Discover_Res(u_int16 cid, bt_sep_info_struct* sep_list, u_int8 result, u_int8 error_code);
void Avdtp_Send_Sep_Capabilities_Get_Res(u_int16 cid, u_int8 codec_type, bt_a2dp_audio_codec_cap_struct* audio_cap);
void Avdtp_Send_Sep_Stream_Config_Res(u_int16 result, u_int8 stream_handle);
void Avdtp_Send_Sep_Stream_Open_Res(u_int16 result, u_int8 stream_handle);
void Avdtp_Send_Sep_Stream_Start_Res(u_int16 result, u_int8 stream_handle);
void Avdtp_Send_Sep_Stream_Close_Res(u_int16 result, u_int8 stream_handle);
void Avdtp_Send_Sep_Stream_Abort_Res( u_int8 stream_handle);
void Avdtp_Send_Sep_Stream_Suspend_Res(u_int16 result, u_int8 stream_handle);

u_int8 avdtp_match_audio_capabilities( bt_a2dp_audio_cap_struct *audio_cap, bt_a2dp_audio_cap_struct *audio_cfg, u_int16 *result );



#ifdef __cplusplus
}
#endif

#endif //AVDTP_H_
